<?php
namespace org\copperforest\oak\resources;
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Request{
    public static $rootGETReference;
    public static $baseURL;
    
    public $resourceName;
    public $id = null;
    
    public $relativeReference;
    public $uniqId;

    
    public $parent = null;
    
    
    function __construct( $uniqId, $relativeReference, $resourceName ){
        $this->uniqId = $uniqId;
        $this->relativeReference = $relativeReference;
        $this->resourceName = $resourceName;
    }
    
    public function GET( $replace = array(), $jsAccept = null, $jsTransform = null ){
         $get = array();
            
        if( isset( $_GET[ $this->uniqId ] ) ){
            $get[ $this->uniqId ] = $_GET[ $this->uniqId ];
        }

        foreach( $replace as $k => $value ){
            $get[ $this->uniqId ][ $k ] = $value;
        }

        $parent = $this->parent;
        while( $parent != null ){
            if( isset( $_GET[ $parent->uniqId ] ) ){
                $get[ $parent->uniqId ] = $_GET[ $parent->uniqId ];
            }
            $parent = $parent->parent;
        }

        $query = '?'. http_build_query( $get ) . '&';

        //En el data-joak-href no tendría que ir toda la query_string solo el del propio ancestor
       // echo CF_HTTP_PATH . self::$rootRelativeReference . $query;
        return  self::$rootGETReference . $query .  ( ( $jsTransform != null )?'" data-joak-href="'. CF_HTTP_PATH . $this->relativeReference . $query . '" data-joak-id="'.$this->uniqId.'" data-joak-transform="' . $jsTransform.'" data-joak-accept="'.(empty($jsAccept)?'':$jsAccept) :'' );        
    }
    
    function stampGETInputName( $name ){
        return htmlspecialchars( $this->uniqId . '[' . $name . ']' );
    }
    
    public function POST( $jsAccept = null, $jsTransform = null ){
        return CF_HTTP_PATH . $this->relativeReference . ( ( $jsTransform != null )?'" data-joak-href="'. CF_HTTP_PATH . $this->relativeReference . '" data-joak-id="'.$this->uniqId.'" data-joak-transform="' . $jsTransform.'" data-joak-accept="'.(empty($jsAccept)?'':$jsAccept) :'' );
    }
    
    
}